package com.example.springstudy.configuration;

import com.example.springstudy.entity.ChartVal;
import com.example.springstudy.services.ChartValRepository;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.concurrent.ThreadLocalRandom;

@Component
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class BgTask {
    @Resource
    private ChartValRepository chartValRepository;

    // 上一次的 湿温度仪 01 的 温度值
    private double last_swdy1_wendu_val = 0;
    // 上一次的 湿温度仪 01 的 湿度值
    private double last_swdy1_shidu_val = 0;
    // 上一次的 深部位移 01 的 值
    private double last_sbwy1_val = 0;
    // 上一次的 深部位移 02 的 值
    private double last_sbwy2_val = 0;

    private double last_qjy1_x_val = 0;
    private double last_qjy2_x_val = 0;
    private double last_qjy3_x_val = 0;
    private double last_qjy4_x_val = 0;
    private double last_qjy5_x_val = 0;
    private double last_qjy6_x_val = 0;
    private double last_qjy7_x_val = 0;
    private double last_qjy8_x_val = 0;
    private double last_qjy9_x_val = 0;
    private double last_qjy10_x_val = 0;

    private double last_qjy1_y_val = 0;
    private double last_qjy2_y_val = 0;
    private double last_qjy3_y_val = 0;
    private double last_qjy4_y_val = 0;
    private double last_qjy5_y_val = 0;
    private double last_qjy6_y_val = 0;
    private double last_qjy7_y_val = 0;
    private double last_qjy8_y_val = 0;
    private double last_qjy9_y_val = 0;
    private double last_qjy10_y_val = 0;


    //3.添加定时任务
    @Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void configureTasks() {
        System.out.println("执行静态定时任务时间 创建 实时数据 : " + LocalDateTime.now());

        // 生成 湿温度仪  数据
        genTemperature();
        // 生成 深部位移  数据
        genWeiYi();
        // 生成 倾角仪  数据 X
        genQingJiaoX();
        // 生成 倾角仪  数据 y
        genQingJiaoY();


    }


    private void genTemperature() {
        // 湿温度仪 01 温度
        double current_swdy1_wendu_val = ThreadLocalRandom.current().nextDouble(10, 34);  // 10~34 度
        ChartVal swdy1_wendu = ChartVal.builder()
                .type("湿温度仪")
                .deviceCode("SWDY-1")
                .deviceName("湿温度仪01")
                .measuringType("温度")
                .Val(current_swdy1_wendu_val)
                .changeVal(current_swdy1_wendu_val - last_swdy1_wendu_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("二级边坡顶部右中")
                .createTime(LocalDateTime.now())
                .build();
        last_swdy1_wendu_val = current_swdy1_wendu_val;
        chartValRepository.save(swdy1_wendu);

        // 湿温度仪 01 湿度
        double current_swdy1_shidu_val = ThreadLocalRandom.current().nextDouble(10, 34);  // 10~34 度
        ChartVal swdy1_shidu = ChartVal.builder()
                .type("湿温度仪")
                .deviceCode("SWDY-1")
                .deviceName("湿温度仪01")
                .measuringType("湿度")
                .Val(current_swdy1_shidu_val)
                .changeVal(current_swdy1_shidu_val - last_swdy1_shidu_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("二级边坡顶部右中")
                .createTime(LocalDateTime.now())
                .build();
        last_swdy1_shidu_val = current_swdy1_shidu_val;
        chartValRepository.save(swdy1_shidu);
    }

    private void genWeiYi() {
        // 深部位移 01
        double current_sbwy1_wendu_val = ThreadLocalRandom.current().nextDouble(-10, 10);  // 10~34 度
        ChartVal sbwy1_wendu = ChartVal.builder()
                .type("深部位移")
                .deviceCode("SBWY-1")
                .deviceName("深部位移01")
                .measuringType("无")
                .Val(current_sbwy1_wendu_val)
                .changeVal(current_sbwy1_wendu_val - last_sbwy1_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("二级边坡顶部右")
                .createTime(LocalDateTime.now())
                .build();
        last_sbwy1_val = current_sbwy1_wendu_val;
        chartValRepository.save(sbwy1_wendu);

        // 深部位移 02
        double current_sbwy2_wendu_val = ThreadLocalRandom.current().nextDouble(2, 10);  // 2~10 mm
        ChartVal sbwy2_wendu = ChartVal.builder()
                .type("深部位移")
                .deviceCode("SBWY-2")
                .deviceName("深部位移02")
                .measuringType("无")
                .Val(current_sbwy2_wendu_val)
                .changeVal(current_sbwy2_wendu_val - last_sbwy2_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("边坡最顶部")
                .createTime(LocalDateTime.now())
                .build();
        last_sbwy2_val = current_sbwy2_wendu_val;
        chartValRepository.save(sbwy2_wendu);

    }

    private void genQingJiaoX() {
        // qjy 01 x 数据
        double current_qjy1_x_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy1_x = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-1")
                .deviceName("倾角仪01")
                .measuringType("X")
                .Val(current_qjy1_x_val)
                .changeVal(current_qjy1_x_val - last_qjy1_x_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("三级边坡顶部左")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy1_x_val = current_qjy1_x_val;
        chartValRepository.save(qjy1_x);
        // qjy 02 x 数据
        double current_qjy2_x_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy2_x = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-2")
                .deviceName("倾角仪02")
                .measuringType("X")
                .Val(current_qjy2_x_val)
                .changeVal(current_qjy2_x_val - last_qjy2_x_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("三级边坡顶部左中")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy2_x_val = current_qjy2_x_val;
        chartValRepository.save(qjy2_x);
        // qjy 03 x 数据
        double current_qjy3_x_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy3_x = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-3")
                .deviceName("倾角仪03")
                .measuringType("X")
                .Val(current_qjy3_x_val)
                .changeVal(current_qjy3_x_val - last_qjy3_x_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("三级边坡顶部右中")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy3_x_val = current_qjy3_x_val;
        chartValRepository.save(qjy3_x);
        // qjy 04 x 数据
        double current_qjy4_x_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy4_x = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-4")
                .deviceName("倾角仪04")
                .measuringType("X")
                .Val(current_qjy4_x_val)
                .changeVal(current_qjy4_x_val - last_qjy4_x_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("三级边坡顶部右")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy4_x_val = current_qjy4_x_val;
        chartValRepository.save(qjy4_x);
        // qjy 05 x 数据
        double current_qjy5_x_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy5_x = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-5")
                .deviceName("倾角仪05")
                .measuringType("X")
                .Val(current_qjy5_x_val)
                .changeVal(current_qjy5_x_val - last_qjy5_x_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("二级边坡顶部左")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy5_x_val = current_qjy5_x_val;
        chartValRepository.save(qjy5_x);
        // qjy 06 x 数据
        double current_qjy6_x_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy6_x = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-6")
                .deviceName("倾角仪06")
                .measuringType("X")
                .Val(current_qjy6_x_val)
                .changeVal(current_qjy6_x_val - last_qjy6_x_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("二级边坡顶部左中")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy6_x_val = current_qjy6_x_val;
        chartValRepository.save(qjy6_x);
        // qjy 07 x 数据
        double current_qjy7_x_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy7_x = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-7")
                .deviceName("倾角仪07")
                .measuringType("X")
                .Val(current_qjy7_x_val)
                .changeVal(current_qjy7_x_val - last_qjy7_x_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("二级边坡顶部右中")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy7_x_val = current_qjy7_x_val;
        chartValRepository.save(qjy7_x);
        // qjy 08 x 数据
        double current_qjy8_x_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy8_x = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-8")
                .deviceName("倾角仪08")
                .measuringType("X")
                .Val(current_qjy8_x_val)
                .changeVal(current_qjy8_x_val - last_qjy8_x_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("二级边坡顶部右")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy8_x_val = current_qjy8_x_val;
        chartValRepository.save(qjy8_x);
        // qjy 09 x 数据
        double current_qjy9_x_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy9_x = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-9")
                .deviceName("倾角仪09")
                .measuringType("X")
                .Val(current_qjy9_x_val)
                .changeVal(current_qjy9_x_val - last_qjy9_x_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("一级边坡顶部左中")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy9_x_val = current_qjy9_x_val;
        chartValRepository.save(qjy9_x);
        // qjy 010 x 数据
        double current_qjy10_x_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy10_x = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-10")
                .deviceName("倾角仪10")
                .measuringType("X")
                .Val(current_qjy10_x_val)
                .changeVal(current_qjy10_x_val - last_qjy10_x_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("一级边坡顶部右中")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy10_x_val = current_qjy10_x_val;
        chartValRepository.save(qjy10_x);
    }
    private void genQingJiaoY() {
        // qjy 01 y 数据
        double current_qjy1_y_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy1_y = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-1")
                .deviceName("倾角仪01")
                .measuringType("Y")
                .Val(current_qjy1_y_val)
                .changeVal(current_qjy1_y_val - last_qjy1_y_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("三级边坡顶部左")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy1_y_val = current_qjy1_y_val;
        chartValRepository.save(qjy1_y);
        // qjy 02 y 数据
        double current_qjy2_y_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy2_y = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-2")
                .deviceName("倾角仪02")
                .measuringType("Y")
                .Val(current_qjy2_y_val)
                .changeVal(current_qjy2_y_val - last_qjy2_y_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("三级边坡顶部左中")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy2_y_val = current_qjy2_y_val;
        chartValRepository.save(qjy2_y);
        // qjy 03 y 数据
        double current_qjy3_y_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy3_y = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-3")
                .deviceName("倾角仪03")
                .measuringType("Y")
                .Val(current_qjy3_y_val)
                .changeVal(current_qjy3_y_val - last_qjy3_y_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("三级边坡顶部右中")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy3_y_val = current_qjy3_y_val;
        chartValRepository.save(qjy3_y);
        // qjy 04 y 数据
        double current_qjy4_y_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy4_y = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-4")
                .deviceName("倾角仪04")
                .measuringType("Y")
                .Val(current_qjy4_y_val)
                .changeVal(current_qjy4_y_val - last_qjy4_y_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("三级边坡顶部右")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy4_y_val = current_qjy4_y_val;
        chartValRepository.save(qjy4_y);
        // qjy 05 y 数据
        double current_qjy5_y_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy5_y = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-5")
                .deviceName("倾角仪05")
                .measuringType("Y")
                .Val(current_qjy5_y_val)
                .changeVal(current_qjy5_y_val - last_qjy5_y_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("二级边坡顶部左")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy5_y_val = current_qjy5_y_val;
        chartValRepository.save(qjy5_y);
        // qjy 06 y 数据
        double current_qjy6_y_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy6_y = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-6")
                .deviceName("倾角仪06")
                .measuringType("Y")
                .Val(current_qjy6_y_val)
                .changeVal(current_qjy6_y_val - last_qjy6_y_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("二级边坡顶部左中")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy6_y_val = current_qjy6_y_val;
        chartValRepository.save(qjy6_y);
        // qjy 07 y 数据
        double current_qjy7_y_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy7_y = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-7")
                .deviceName("倾角仪07")
                .measuringType("Y")
                .Val(current_qjy7_y_val)
                .changeVal(current_qjy7_y_val - last_qjy7_y_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("二级边坡顶部右中")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy7_y_val = current_qjy7_y_val;
        chartValRepository.save(qjy7_y);
        // qjy 08 y 数据
        double current_qjy8_y_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy8_y = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-8")
                .deviceName("倾角仪08")
                .measuringType("Y")
                .Val(current_qjy8_y_val)
                .changeVal(current_qjy8_y_val - last_qjy8_y_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("二级边坡顶部右")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy8_y_val = current_qjy8_y_val;
        chartValRepository.save(qjy8_y);
        // qjy 09 y 数据
        double current_qjy9_y_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy9_y = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-9")
                .deviceName("倾角仪09")
                .measuringType("Y")
                .Val(current_qjy9_y_val)
                .changeVal(current_qjy9_y_val - last_qjy9_y_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("一级边坡顶部左中")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy9_y_val = current_qjy9_y_val;
        chartValRepository.save(qjy9_y);
        // qjy 010 y 数据
        double current_qjy10_y_val = ThreadLocalRandom.current().nextDouble(0.02, 0.2);  // 0.02~0.2 度
        ChartVal qjy10_y = ChartVal.builder()
                .type("倾角仪")
                .deviceCode("QJY-10")
                .deviceName("倾角仪10")
                .measuringType("Y")
                .Val(current_qjy10_y_val)
                .changeVal(current_qjy10_y_val - last_qjy10_y_val)
                .valStatus("正常")
                .dataIntegrityRate(100.0)
                .remark("一级边坡顶部右中")
                .createTime(LocalDateTime.now())
                .build();
        last_qjy10_y_val = current_qjy10_y_val;
        chartValRepository.save(qjy10_y);
    }

}
